<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>超大数字相加</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f6f9;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .container {
            background-color: #fff;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 500px;
            text-align: center;
        }

        h1 {
            color: #333;
            font-size: 24px;
            margin-bottom: 20px;
        }

        .input-group {
            margin-bottom: 15px;
        }

        .input-group input {
            width: 80%;
            padding: 10px;
            font-size: 16px;
            border: 1px solid #ddd;
            border-radius: 5px;
            margin-right: 10px;
            margin-bottom: 10px;
        }

        .input-group button {
            padding: 10px 20px;
            font-size: 16px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        .input-group button:hover {
            background-color: #45a049;
        }

        .result {
            margin-top: 20px;
            padding: 10px;
            background-color: #f1f1f1;
            border: 1px solid #ddd;
            border-radius: 5px;
            font-size: 18px;
            color: #333;
        }

        .result:empty {
            padding: 0;
            border: none;
        }

        .error {
            color: red;
            margin-top: 15px;
        }
    </style>
</head>

<body>

    <div class="container">
        <h1>超大数字相加</h1>
        <div class="input-group">
            <input type="text" id="numA" placeholder="请输入第一个超大数字" />
            <input type="text" id="numB" placeholder="请输入第二个超大数字" />
            <button onclick="calculateSum()">计算</button>
        </div>
        <div class="result" id="result"></div>
        <div class="error" id="error"></div>
    </div>

    <script>
        function bigNumberSum(a, b) {
            let aArr = a.split(''),
                bArr = b.split('');
            let maxLen = Math.max(aArr.length, bArr.length);
            let flag = false,
                res = [];

            for (let i = 0; i < maxLen; i++) {
                let nPopA = aArr.pop(),
                    nPopB = bArr.pop();
                let numA = parseInt(nPopA ? nPopA : 0),
                    numB = parseInt(nPopB ? nPopB : 0);
                let unit = flag ? numA + numB + 1 : numA + numB;

                if (parseInt(unit / 10) > 0) {
                    res.push(unit % 10);
                    flag = true;
                } else {
                    res.push(unit);
                    flag = false;
                }

                if (flag && i === maxLen - 1) {
                    res.push(1);
                }
            }

            return res.reverse().join('');
        }

        function calculateSum() {
            const numA = document.getElementById('numA').value.trim();
            const numB = document.getElementById('numB').value.trim();

            if (!numA || !numB) {
                document.getElementById('error').textContent = '请填写两个超大数字';
                document.getElementById('result').textContent = '';
                return;
            }

            // 清除错误提示
            document.getElementById('error').textContent = '';

            // 调用 bigNumberSum 函数
            const sum = bigNumberSum(numA, numB);

            // 显示计算结果
            document.getElementById('result').textContent = `结果是：${sum}`;
        }
    </script>

</body>

</html>